package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.place.EndTurnPlace;
import com.real8.romemp.client.place.PoliticalPlace;
import com.real8.romemp.client.ui.PoliticalView;
import com.real8.romemp.client.ui.PoliticalView.Presenter;
import com.real8.romemp.shared.GameData;

public class PoliticalActivity extends AbstractActivity implements Presenter {
	private final ClientFactory clientFactory;
	private PoliticalView view;

	public PoliticalActivity(PoliticalPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getPoliticalView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();
			view.showPolitics();

			// check popularity
			final GameData gameData = clientFactory.getGameData();
			if (gameData.getVF_popularity() >= 0) {
				// show popular
				view.showPopular();

				// show continue
				view.showContinue();
			} else {
				// show not popular
				view.showNotPopular(-gameData.getVF_popularity());

				// check treasury
				if (gameData.getTR_treasury() < 1000) {
					// show continue
					view.showContinue();
				} else {
					// show appease
					view.showAppeaseQ();
				}
			}

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	@Override
	public void onContinue() {
		goToNextPlace();
	}

	@Override
	public void onAppeaseAnswer(int amount) {
		int boughtPoints = amount / 1000;
		int goldSpend = boughtPoints * 1000;
		if (boughtPoints > 0) {
			GameData gameData = clientFactory.getGameData();
			gameData.setTR_treasury(gameData.getTR_treasury() - goldSpend);
			gameData.setVF_popularity(gameData.getVF_popularity()
					+ boughtPoints);
		}

		goToNextPlace();
	}

	private void goToNextPlace() {
		clientFactory.getPlaceController().goTo(new EndTurnPlace());
	}
}
